nums=[-55,-24,-18,-11,-7,-3,4,5,6,9,11,23,33]
target=0

def threeSumClosest(nums,target):
    #对待排序列进行排序
    nums.sort()
    print(nums)
    closest=10000
    closest_sum=0
    i=0
    while i<len(nums)-2:
        print("第{0}轮".format(i+1))
        #最后一次循环保证存在3个数
        l_pointer,r_pointer=i+1,len(nums)-1
        closet = 10000
        closet_sum=0
        while l_pointer<r_pointer:
            res_sum = nums[l_pointer] + nums[r_pointer] + nums[i]
            res_sub=res_sum-target
            print("res_sub:",res_sub)
            if res_sub>0:
                r_pointer-=1
            elif res_sub<0:
                l_pointer+=1
            else:
                #最接近的数0，找到之后可以直接返回结果
                # print("找到值：",target)
                return target
            if abs(res_sub)<=closet:
                closet=abs(res_sub)
                closet_sum=res_sum
                # print("res_sum:",res_sum)
                # print("closet:",closet)
        if closest>closet:
            closest=closet
            closest_sum=closet_sum
        i+=1
    # print("closet_sum:",closest_sum)
    return closest_sum

def threeSumClosest_1(nums,target):
    nums.sort()
    print(nums)
    closest=10000
    closest_sum=0
    i=0
    while i<len(nums)-2:
        print("-----------、第",i+1,"轮------------、-")
        print("nums[i]:",nums[i])
        l_pointer,r_pointer=i+1,len(nums)-1
        sub_closest=10000
        sub_closest_sum=0
        while l_pointer<r_pointer:
            res_sum=nums[l_pointer]+nums[r_pointer]+nums[i]
            res_sub=res_sum-target
            if res_sub>0:
                r_pointer-=1
            elif res_sub<0:
                l_pointer+=1
            else:
                print("找到了,",target)
                return target
            if abs(res_sub)<=sub_closest:
                sub_closest=abs(res_sub)
                sub_closest_sum=res_sum
                print("sub_closest:",sub_closest)
                print(sub_closest_sum)
        if closest>sub_closest:
            closest=sub_closest
            closest_sum=sub_closest_sum
            print("closest:",closest)
            print("closest_sum:",closest_sum)
        else:
            print("closest不变",closest,closest_sum)
        i+=1
    return closest_sum
    pass
print(threeSumClosest_1(nums,target))